import Vue from 'vue';
//引入状态管理
import Vuex from 'vuex';
import userDATA from './modules/userDATA';
import commonDATA from './modules/commonDATA';
import router, { asyncRouterMap } from "../router/index";
import api from '@/api/HttpIndex.js' // 导入api接口

//调用
Vue.use(Vuex);
export default new Vuex.Store({
    modules: {
        //获取数据的写法
        //this.$store.state.userDATA.数据名称
        userDATA,
        commonDATA
    },
    mutations: {
        //使用的写法
        // this.$store.commit('调用名称',data)
        //userDATA commonDATA 
        UserUpdata(state, arg) {
            state.userDATA.name = arg.name
            state.userDATA.token = arg.token
            state.userDATA.isroles = arg.isroles
            state.userDATA.dataList.title = '欢迎' + arg.name
        },
        userLoginUpdata(state, arg) {
            state.userDATA.isroles = arg
        },
        updataOrder(state, arg) {
            //用户订单
            state.userDATA.order = arg
        },
        updataCodeKey(state,arg){
            //验证码唯一标识
            state.userDATA.codeKey = arg
        },
        UserLogout(state) {
            localStorage.clear()
            state.userDATA.token = ''
            state.userDATA.roles = ''
            state.userDATA.routerList = []
            state.userDATA.sideNavigation = []
            state.userDATA.dataList.title = ''
            state.userDATA.isroles = false
        },
        userRolesUpdata(state, arg) {
            //写入数据
            api.userAPI.GetInformation().then(res=>{
                state.userDATA.roles = res.data.role
                state.userDATA.name = res.data.name
                state.userDATA.porteait = res.data.porteait
                state.userDATA.gender = res.data.gender
                state.userDATA.email = res.data.email
                state.userDATA.dataList.title = '欢迎: ' + res.data.name
                location.reload();
            })
        },
        userisrolesUpdata(state, arg) {
            state.userDATA.isroles = arg
        },
        UserRouterF5Updata(state, arg) {
            router.addRoutes(state.userDATA.routerList);
            // state.userDATA.isroles = arg
        },
        UserRouterUpdata(state) {
            var userRoles = state.userDATA.roles;
            console.log('store同步时的Roles:', userRoles)
            var routesData = [];
            state.userDATA.routerList = []
            state.userDATA.sideNavigation = []
            for (var k = 0, len2 = asyncRouterMap.length; k < len2; k++) {
                for (
                    var l = 0, len3 = asyncRouterMap[k].meta.role.length;
                    l < len3;
                    l++
                ) {
                    if (userRoles == asyncRouterMap[k].meta.role[l]) {
                        //判断用户的身份在路由里面存在不
                        routesData.push(asyncRouterMap[k]);
                        if (asyncRouterMap[k].children && asyncRouterMap[k].children.length > 0 && asyncRouterMap[k].label == "用户") {
                            for (var m = 0, len4 = asyncRouterMap[k].children.length; m < len4; m++) {
                                var neilist = []  //保存三层嵌套路由
                                if (asyncRouterMap[k].children[m].children) {
                                    for (var m1 = 0, len41 = asyncRouterMap[k].children[m].children.length; m1 < len41; m1++) {
                                        neilist.push(
                                            {
                                                'name': asyncRouterMap[k].children[m].children[m1].name,
                                                'label': asyncRouterMap[k].children[m].children[m1].label,
                                            }
                                        )
                                    }
                                }
                                state.userDATA.sideNavigation.push({
                                    'label': asyncRouterMap[k].children[m].label,
                                    'name': asyncRouterMap[k].children[m].name,
                                    'children': neilist
                                })
                            }
                            // console.log(state.userDATA.sideNavigation)

                        }


                    }
                }
            }
            state.userDATA.routerList.push(...routesData)
            router.addRoutes(routesData);
            // console.log('UserRouterUpdata 被动执行路由渲染', state.userDATA.routerList, router, routesData)
        },
        cardataUpdata(state, arg) {
            state.userDATA.cardata.name = arg.name
        },
        setRouterList(state, arg) {
            // console.log('执行setRouterList')
            state.userDATA.routerList.push(arg)
        },
        //commonDATA
        GetSideberUpdata(state, arg) {
            state.commonDATA.GetSideber = arg
        },
        //商品筛选页面
        electricCarsClean(state) {
            var arg = ''
            state.commonDATA.typeSelection = arg
            state.commonDATA.orderStatus = arg
            state.commonDATA.priceMax = arg
            state.commonDATA.priceMin = arg
            sessionStorage.removeItem("typeSelection");
            sessionStorage.removeItem("orderStatus");
            sessionStorage.removeItem("JGmin");
            sessionStorage.removeItem("JGmax");
            sessionStorage.removeItem("JGflg");
            sessionStorage.removeItem("ClassificationList");
            // console.log('清除电动车页面数据完成')
        },
        //更新电动车导航内容
        updataGroDATA(state, arg) {
            state.commonDATA.groDATA = arg

        },
        updataCarsMgsList(state, arg) {
            state.commonDATA.carsMgsList = arg
        },
        typeSelectionUpdata(state, arg) {
            //电动车选择的类型
            state.commonDATA.typeSelection = arg
            sessionStorage.setItem("typeSelection", JSON.stringify(arg));
        },
        orderStatusUpdata(state, arg) {
            //排序类型
            state.commonDATA.orderStatus = arg
            sessionStorage.setItem("orderStatus", JSON.stringify(arg));
        },
        priceUpdata(state, arg) {
            //价格区间
            state.commonDATA.priceMax = arg.priceMax
            state.commonDATA.priceMin = arg.priceMin
            sessionStorage.setItem("priceMax", JSON.stringify(arg.priceMax));
            sessionStorage.setItem("priceMin", JSON.stringify(arg.priceMin));
        },
        pageNumberUpdata(state, arg) {
            //电动车页码更新
            state.commonDATA.pageNumber = arg
        },

        getlistApiUpadta(state, arg) {
            //处理后台返回的最新筛选属性信息
            state.commonDATA.getlistAPI = arg
        },
        pageUpdata(state, arg) {
            state.commonDATA.total = arg.total//一共的数据
            state.commonDATA.list = arg.list//商品数据列表
            state.commonDATA.pageNum = arg.pageNum//当前页码
            state.commonDATA.pageSize = arg.pageSize//一页多少条数据
        },
        pageNumUpdata(state, arg) {
            state.commonDATA.pageNum = arg//当前页码
        },
        //商城页面
        MainProductsVuexUpdata(state, arg) {
            //商城主要的大块商品数据
            // console.log(arg)
            state.commonDATA.MainProductsVuex = arg
        },



    },
    actions: {
        //购物车
        getProXa() { //获取购物车商品
            return new Promise((resolve, reject) => {
                try {
                    api.orderAPI.getPro().then(res => {
                        const data = res.data
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
    },
})
